package org.jeecg.modules.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.core.constant.YesOrNotEnum;
import org.jeecg.modules.maps.model.param.BuildFloorQuery;
import org.jeecg.modules.project.entity.BimProjectBuild;
import org.jeecg.modules.project.entity.BimProjectFloor;
import org.jeecg.modules.project.entity.BuildResultType;
import org.jeecg.modules.project.entity.ProjectInfo;
import org.jeecg.modules.project.mapper.ProjectInfoMapper;
import org.jeecg.modules.project.model.result.BimProjectBuildInfo;
import org.jeecg.modules.project.model.result.BimProjectFloorResult;
import org.jeecg.modules.project.model.result.ProjectInfoResult;
import org.jeecg.modules.project.service.IBimProjectBuildService;
import org.jeecg.modules.project.service.IBimProjectFloorService;
import org.jeecg.modules.project.service.IProjectInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: bim_project_info
 * @Author: jeecg-boot
 * @Date: 2022-11-25
 * @Version: V1.0
 */
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements IProjectInfoService {

    @Autowired
    private IBimProjectBuildService bimProjectBuildService;

    @Autowired
    private IBimProjectFloorService bimProjectFloorService;

    @Override
    public void saveProj(ProjectInfo projectInfo) {
        //查询第三方项目id是否存在
        ProjectInfo project = this.findByProj(projectInfo);
        if (Objects.nonNull(project)) {
            throw new JeecgBootException("项目已经存在绑定关系");
        }
        //工程管理编码不为空则为已添加编辑
        if (ObjectUtil.isNotNull(projectInfo.getId())) {
            projectInfo.setDelFlag(YesOrNotEnum.N.getCode());
            projectInfo.setEnableFlag(YesOrNotEnum.Y.getCode());
            this.updateById(projectInfo);
        } else {
            projectInfo.setDelFlag(YesOrNotEnum.N.getCode());
            projectInfo.setEnableFlag(YesOrNotEnum.Y.getCode());
            this.save(projectInfo);
        }
    }

    @Override
    public ProjectInfo findByProj(ProjectInfo projectInfo) {
        List<ProjectInfo> projectInfos = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(ProjectInfo::getAppCode, projectInfo.getAppCode())
                .eq(ProjectInfo::getProjectId, projectInfo.getProjectId())
                .orderByDesc(ProjectInfo::getId)
                .list();
        if (CollUtil.isNotEmpty(projectInfos)) {
            return projectInfos.get(0);
        }
        return null;
    }

    //获取工程信息及属性楼栋楼层信息
    @Override
    public ProjectInfoResult getProjModel(Integer projectInfoId) {
        ProjectInfoResult infoResult = new ProjectInfoResult();
        ProjectInfo projectInfo = this.getById(projectInfoId);
        BeanUtils.copyProperties(projectInfo, infoResult);
        List<BimProjectBuildInfo> targetBuildList = getProjBuildInfos(projectInfoId,BuildResultType.COMMON);
        if(CollUtil.isEmpty(targetBuildList)){
            targetBuildList = new ArrayList<>();
        }
        infoResult.setModelBuilds(JSONUtil.parseArray(targetBuildList));
        return infoResult;
    }

    @Override
    public List<BimProjectBuildInfo> getProjBuildInfos(Integer projectInfoId, BuildResultType resultType) {
        BuildFloorQuery query = new BuildFloorQuery();
        query.setProjectInfoId(projectInfoId);
        return this.getProjBuildInfos(query,resultType);
    }

    @Override
    public List<BimProjectBuildInfo> getProjBuildInfos(BuildFloorQuery query, BuildResultType resultType){
        BimProjectBuild projectBuild = new BimProjectBuild();
        projectBuild.setProjInfoId(query.getProjectInfoId());
        if(ObjectUtil.isNotNull(query.getBuildId())){
            BimProjectBuild targetProjectBuild =  bimProjectBuildService.getById(query.getBuildId());
            projectBuild.setProjInfoId(targetProjectBuild.getProjInfoId());
            projectBuild.setBuildId(targetProjectBuild.getBuildId());
        }
        if(ObjectUtil.isNotNull(query.getFloorId())){
            BimProjectFloor targetProjectFloor =  bimProjectFloorService.getById(query.getFloorId());
            BimProjectBuild targetProjectBuild =  bimProjectBuildService.getById(targetProjectFloor.getBuildId());
            projectBuild.setProjInfoId(targetProjectBuild.getProjInfoId());
            projectBuild.setBuildId(targetProjectBuild.getBuildId());
        }
        List<BimProjectBuild> projectBuildList = bimProjectBuildService.queryBuild(projectBuild);
        List<BimProjectBuildInfo> targetBuildList = new ArrayList<>();
        if (CollUtil.isNotEmpty(projectBuildList)) {
            projectBuildList.stream().forEach(item -> {
                BimProjectBuildInfo buildResult = new BimProjectBuildInfo();
                BeanUtils.copyProperties(item, buildResult);
                BimProjectFloor floorSelector = new BimProjectFloor();
                floorSelector.setBuildId(item.getBuildId());
                if(ObjectUtil.isNotNull(query.getFloorId())){
                    floorSelector.setFloorId(query.getFloorId());
                }
                buildResult.setFloors(this.convert(bimProjectFloorService.queryFloor(floorSelector, resultType)));
                targetBuildList.add(buildResult);
            });
        }
        return targetBuildList;
    }

    private List<BimProjectFloorResult> convert(List<BimProjectFloor> queryFloors) {
        List<BimProjectFloorResult> floorResults = new ArrayList<>();
        if(CollUtil.isNotEmpty(queryFloors)){
            queryFloors.stream().forEach(item ->{
                BimProjectFloorResult floorResult = new BimProjectFloorResult();
                floorResult.setBuildId(item.getBuildId());
                floorResult.setFloorId(item.getFloorId());
                floorResult.setFloorNo(item.getFloorNo());
                if(StringUtils.isNotBlank(item.getMaps())) {
                    floorResult.setMaps(JSONUtil.parseObj(item.getMaps()));
                }
                floorResults.add(floorResult);
            });
        }
        return floorResults;
    }

}
